package com.eco.controller;


import com.eco.util.Result;
import com.eco.util.SysConfig;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("file")
@CrossOrigin
public class FileUploadController {
@RequestMapping("upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {

        //上传文件到指定路径
        String oldName = file.getOriginalFilename();//原来的名字
        String lastName = oldName.substring(oldName.lastIndexOf("."));//.txt等
        String newName = UUID.randomUUID().toString()+lastName;
        File target = new File(SysConfig.UPLOAD_DIR+"/"+newName);
        file.transferTo(target);
        //返回
        String path = SysConfig.UPLOAD_PATH+newName;
        return Result.success("上传成功",path);
    }
}

























